/* -----------------------------------------------------------------------------
*  CString
*  Author: Antonio Javier Gallego Sánchez (ajgallego AT gmail DOT com)
*  License: GNU Public License
* ----------------------------------------------------------------------------*/

#include "CString.hpp"

#include <sstream>
#include <algorithm>
#include <string.h>
#include <cctype>

#include "CVectorString.hpp"
#include "asrtbas.h"

//------------------------------------------------------------------------------
//
const char* CString :: vectorToString( const class CVectorString *strList, char separator )
{
    assert_no_null( strList );

    string result = "";

    unsigned int size = strList->size();

    for( unsigned int i = 0; i < size; ++i )
    {
        if( i > 0 )
            result += string( 1, separator );

        result += string( strList->get( i ) );
    }

    return strdup( result.c_str() );
}

//------------------------------------------------------------------------------
//
const char* CString :: stringToUpper( const char* str )
{
    string aux( str );

    std::transform( aux.begin(), aux.end(),
                    aux.begin(), ::toupper);

    return strdup( aux.c_str() );
}

//------------------------------------------------------------------------------
// 
const char* CString :: stringToLower( const char* str )
{
    string aux( str );

    std::transform( aux.begin(), aux.end(),
                    aux.begin(), ::tolower);

    return strdup( aux.c_str() );
}

//------------------------------------------------------------------------------
// 
int CString :: charToInt( char c )
{
    if ((c >= '0') && (c <= '9'))
        return c - '0';

    return -1;
}

//------------------------------------------------------------------------------
// 
string CString :: charToString( char c )
{
    return string(1, c);
}

//------------------------------------------------------------------------------
// 
string CString :: intToString( int value )
{
    ostringstream out;
    out << value;
    return out.str();
}

//------------------------------------------------------------------------------
// 
string CString :: floatToString( float value )
{
    ostringstream out;
    out << value;
    return out.str();
}

//------------------------------------------------------------------------------
//
string CString :: doubleToString( double value )
{
    ostringstream out;
    out << value;
    return out.str();
}

//------------------------------------------------------------------------------
//
bool CString :: stringToInt( const char* str, int *num )
{
    std::istringstream iss(str);
    iss >> noskipws >> *num;
    return iss.eof() && !iss.fail();
}

//------------------------------------------------------------------------------
//
bool CString :: stringToFloat( const char* str, float *num )
{
    std::istringstream iss(str);
    iss >> noskipws >> *num;
    return iss.eof() && !iss.fail();
}

//------------------------------------------------------------------------------
//
bool CString :: stringToDouble( const char* str, double *num )
{
    std::istringstream iss(str);
    iss >> noskipws >> *num;
    return iss.eof() && !iss.fail();
}

//------------------------------------------------------------------------------
//
bool CString :: stringToHex( const char* str, int *num )
{
    std::istringstream iss(str);
    iss >> noskipws >> hex >> *num;
    return iss.eof() && !iss.fail();
}

//------------------------------------------------------------------------------
//
bool CString :: stringToOct( const char* str, int *num )
{
    std::istringstream iss(str);
    iss >> noskipws >> oct >> *num;
    return iss.eof() && !iss.fail();
}

//------------------------------------------------------------------------------
//
bool CString :: isInteger( const char* str )
{
    int aux;
    return stringToInt( str, &aux );
}

//------------------------------------------------------------------------------
//
bool CString :: isFloat( const char* str )
{
    float aux;
    return stringToFloat( str, &aux );
}

//------------------------------------------------------------------------------
//
string CString :: getSuffix( const string &str, unsigned int numChars )
{
    return string(str, ( numChars > str.size() )? 0 : str.size() - numChars );
}

//------------------------------------------------------------------------------
//
string CString :: getPreffix( const string &str, unsigned int numChars )
{
    return string(str, 0, ( numChars > str.size() )? str.size() : numChars );
}

//------------------------------------------------------------------------------
//
void CString :: substChar( string *text, char c1, char c2 )
{
    for( string::iterator it = (*text).begin(); it != (*text).end(); it++) {
        if (*it == c1)
            *it = c2;
    }
}

//------------------------------------------------------------------------------
//
string CString :: trim( const string &str )
{
    string aux;
    aux = leftTrim( str );
    aux = rightTrim( aux );
    return aux;
}

//------------------------------------------------------------------------------
//
string CString :: leftTrim( const string &str )
{
    string aux = str;
    string::iterator it;

    for( it = aux.begin(); it != aux.end() && isspace(*it); ++it );

    aux.erase( aux.begin(), it );

    return aux;
}

//------------------------------------------------------------------------------
//
string CString :: rightTrim( const string &str )
{
    string aux = str;
    string::iterator it;

    for( it = aux.end()-1; it != aux.begin() && isspace(*it); --it );

    aux.erase( it+1, aux.end() );

    return aux;
}

/**
*/